package org.yenn.pattern.aliyun.behavioral.interpreter;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.yenn.pattern.aliyun.behavioral.interpreter.impl.AndExpression;
import org.yenn.pattern.aliyun.behavioral.interpreter.impl.OrExpression;
import org.yenn.pattern.aliyun.behavioral.interpreter.impl.TerminalExpression;

import static org.junit.Assert.*;

/**
 * 步骤 3
 * InterpreterPatternDemo 使用 Expression 类来创建规则，并解析它们。
 */
@Slf4j
public class InterpreterPatternDemo {
    //规则：Robert 和 John 是男性
    public static Expression getMaleExpression() {
        Expression robert = new TerminalExpression("Robert");
        Expression john = new TerminalExpression("John");
        return new OrExpression(robert, john);
    }

    //规则：Julie 是一个已婚的女性
    public static Expression getMarriedWomanExpression() {
        Expression julie = new TerminalExpression("Julie");
        Expression married = new TerminalExpression("Married");
        return new AndExpression(julie, married);
    }

    /**
     * 步骤 4
     * 验证输出。
     *
     * 2021-06-24 14:42:23,438 [main] [org.yenn.pattern.aliyun.behavioral.interpreter.InterpreterPatternDemo.interpret(InterpreterPatternDemo.java:42)] - [INFO]
     * isMale=OrExpression(expr1=TerminalExpression(data=Robert), expr2=TerminalExpression(data=John))
     * 2021-06-24 14:42:23,442 [main] [org.yenn.pattern.aliyun.behavioral.interpreter.InterpreterPatternDemo.interpret(InterpreterPatternDemo.java:43)] - [INFO]
     * isMarriedWoman=AndExpression(expr1=TerminalExpression(data=Julie), expr2=TerminalExpression(data=Married))
     * John is male? true
     * Julie is a married women? true
     */
    @Test
    public void interpret() {
        Expression isMale = getMaleExpression();
        Expression isMarriedWoman = getMarriedWomanExpression();
        log.info("isMale={}",isMale);
        log.info("isMarriedWoman={}",isMarriedWoman);

        System.out.println("John is male? " + isMale.interpret("John"));
        System.out.println("Julie is a married women? "  + isMarriedWoman.interpret("Married Julie"));
    }
}